<template xmlns="http://www.w3.org/1999/html">
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.keyword" placeholder="手机号/ID" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-select v-model="listQuery.anchor" placeholder="主播" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option label="是" value="1" />
        <el-option label="否" value="0" />
      </el-select>
      <el-select v-model="listQuery.sex" placeholder="性别" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option label="未知" value="0" />
        <el-option label="男" value="1" />
        <el-option label="女" value="2" />
      </el-select>
      <el-select v-model="listQuery.is_real" placeholder="在线认证" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option label="已认证" value="1" />
        <el-option label="未认证" value="0" />
      </el-select>
      <el-select v-model="listQuery.is_online" placeholder="在线" style="width: 140px" class="filter-item" @change="handleFilter">
        <el-option label="在线" value="1" />
        <el-option label="未在线" value="0" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="{row}">
          <a class="click_a" href="javascript:;" onclick="drawer = true">{{ row.id }}</a>
        </template>
      </el-table-column>
      <el-table-column label="昵称" min-width="120" align="center">
        <template slot-scope="{row}">
          <a class="click_a" href="javascript:;">{{ row.nickname }}</a>
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center">
        <template slot-scope="{row}">
          <el-image :src="row.head_img" :preview-src-list="[row.head_img]" class="dialog_img" />
        </template>
      </el-table-column>
      <el-table-column label="手机" min-width="120" align="center">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center">
        <template slot-scope="{row}">
          <span>{{ row.sex | formatSex }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金币" align="center">
        <template slot-scope="{row}">
          <span>{{ row.coins }}</span>
        </template>
      </el-table-column>
      <el-table-column label="余额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.money }}</span>
        </template>
      </el-table-column>
      <el-table-column label="相册" align="center">
        <template slot-scope="{row}">
          <a class="click_a" @click="getUserPhotoAlbum(row)">查看</a>
        </template>
      </el-table-column>
      <el-table-column label="设备" prop="created_at" sortable="custom" align="center" min-width="150">
        <template slot-scope="{row}">
          <span>平台：{{ row.platform }}</span>
          <br>
          <span>版本：{{ row.app_version }}</span>
          <br>
          <span>注册：{{ row.created_at | formatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="在线/离线" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.last_login_at | formatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="今日收益" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.last_login_ip }}</span>
        </template>
      </el-table-column>
      <el-table-column label="微信号" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.wechat }}</span>
        </template>
      </el-table-column>
      <el-table-column label="封禁时间" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <span>{{ row.disabled_at | formatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="180" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <a class="click_a" @click="getUserInfo(row)">
            编辑
          </a>
          <a class="click_a" @click="getUserOrder(row)">
            订单
          </a>
          <a class="click_a" @click="getUserCoinsLog(row)">
            流水
          </a>
          <a class="click_a" @click="getUserMoneyLog(row)">
            收入
          </a>
          <br>
          <a class="click_a" @click="getChatList(row)">
            聊天
          </a>
          <a class="click_a" @click="getUserInfo(row)">
            封号
          </a>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <div class="components-container">
      <el-dialog v-el-drag-dialog :visible.sync="dialogVisibleUserInfo" title="用户详情" width="35%" top="0">
        <el-form label-width="80px" label-position="left">
          <el-form-item label="昵称">
            <span>{{ userInfo.nickname }}</span>
          </el-form-item>
          <el-form-item label="简介">
            <span>{{ userInfo.nickname }}</span>
          </el-form-item>
          <el-form-item label="微信">
            <span>{{ userInfo.wechat ? userInfo.wechat : '未绑定' }}</span>
          </el-form-item>
          <el-form-item label="真人">
            <span>{{ userInfo.is_real ? '已认证' : '未认证' }}</span>
          </el-form-item>
          <el-form-item label="主播">
            <span>{{ userInfo.is_anchor ? '是' : '否' }}</span>
          </el-form-item>
          <el-form-item label="在线">
            <span>{{ userInfo.is_online ? '在线' : '离线' }}</span>
          </el-form-item>
          <el-form-item label="性别">
            <span>{{ userInfo.sex | formatSex }}</span>
          </el-form-item>
          <el-form-item label="生日">
            <span>{{ userInfo.birthday }}</span>
          </el-form-item>
          <el-form-item label="城市">
            <span>{{ userInfo.city }}</span>
          </el-form-item>
          <el-form-item label="注册时间">
            <span>{{ userInfo.created_at | formatTime }}</span>
          </el-form-item>
        </el-form>
      </el-dialog>

      <el-dialog v-el-drag-dialog :visible.sync="dialogVisibleUserAlbum" title="用户相册" width="35%">
        <el-image v-for="album in userPhotoAlbum" :src="album" :preview-src-list="userPhotoAlbum" class="dialog_img" style="width: 100px;margin-right: 10px" />
      </el-dialog>
    </div>
    <div class="components-container">
      <el-drawer
        v-drawerDrag
        :visible.sync="drawerVisibleOrder"
        size="1200px"
        title="订单"
      >
        <UserOrder :user_id.sync="currentUserId" />
      </el-drawer>
    </div>
    <div class="components-container">
      <el-drawer
        v-drawerDrag
        :visible.sync="drawerVisibleCoins"
        size="1200px"
        title="金币日志"
      >
        <CoinsLog :user_id.sync="currentUserId" />
      </el-drawer>
    </div>
    <div class="components-container">
      <el-drawer
        v-drawerDrag
        :visible.sync="drawerVisibleMoney"
        size="1200px"
        title="余额日志"
      >
        <MoneyLog :user_id.sync="currentUserId" />
      </el-drawer>
    </div>
    <div class="components-container">
      <el-drawer
        v-drawerDrag
        :visible.sync="drawerVisibleChat"
        size="85%"
        title="聊天信息"
      >
        <Chat :user_id.sync="currentUserId" />
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { userList } from '@/api/user'
import { userPhotoAlbum } from '@/api/photo-album'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import elDragDialog from '@/directive/el-drag-dialog' // base on element-ui
import UserOrder from '@/views/order/list' // base on element-ui
import CoinsLog from '@/views/coinsLog/list' // base on element-ui
import MoneyLog from '@/views/moneyLog/list' // base on element-ui
import Chat from '@/views/chat/list' // base on element-ui

export default {
  name: 'ComplexTable',
  components: { Pagination, UserOrder, CoinsLog, MoneyLog, Chat },
  directives: { waves, elDragDialog },
  filters: {
    statusText(status) {
      const statusMap = {
        0: '禁用',
        1: '正常'
      }
      return statusMap[parseInt(status)]
    },
    formatTime: function(time) {
      if (parseInt(time) > 0) {
        return parseTime(parseInt(time), '{y}-{m}-{d} {h}:{i}')
      }
      return '-'
    },
    formatSex: function(sex) {
      const sexMap = {
        0: '未知',
        1: '男',
        2: '女'
      }
      return sexMap[parseInt(sex)]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        importance: undefined,
        username: undefined,
        status: undefined,
        sort: 'desc',
        sort_field: 'id',
        keyword: '',
        anchor: '',
        is_online: '',
        is_real: '',
        sex: ''
      },
      rules: {
        username: [{ required: true, message: '请输入账号', trigger: 'change' }]
      },
      downloadLoading: false,
      dialogVisibleUserInfo: false,
      dialogVisibleUserAlbum: false,
      drawerVisibleOrder: false,
      drawerVisibleCoins: false,
      drawerVisibleMoney: false,
      drawerVisibleChat: false,
      userInfo: {},
      currentUserId: '',
      userPhotoAlbum: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      userList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total ? response.data.total : 0
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    sortChange(data) {
      this.listQuery.sort_field = data.prop
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      this.handleFilter()
    },
    getSortClass: function() {
      this.listQuery.sort = this.listQuery.sort === 'asc' ? 'desc' : 'asc'
      return this.listQuery.sort === 'asc' ? 'ascending' : 'descending'
    },
    getUserInfo: function(item) {
      this.userInfo = item
      this.dialogVisibleUserInfo = true
    },
    getUserPhotoAlbum: function(item) {
      console.log(item.id)
      this.dialogVisibleUserAlbum = true
      userPhotoAlbum({ 'user_id': item.id }).then(response => {
        this.userPhotoAlbum = response.data
      })
    },
    getUserOrder: function(item) {
      this.currentUserId = item.id
      this.drawerVisibleOrder = true
    },
    getUserCoinsLog: function(item) {
      this.currentUserId = item.id
      this.drawerVisibleCoins = true
    },
    getUserMoneyLog: function(item) {
      this.currentUserId = item.id
      this.drawerVisibleMoney = true
    },
    getChatList: function(item) {
      this.currentUserId = item.id
      this.drawerVisibleChat = true
    }
  }
}
</script>

<style>
.click_a {
  color: #409EFF;
  padding-right: 5px;
}
</style>
